package com.accesshollywood.grfxOrdering.Dialogs;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.SQL.IndexedSQL;
import com.accesshollywood.SQL.GrfxDateTime;
import com.accesshollywood.grfxOrdering.ComboBoxPlus;
import com.accesshollywood.grfxOrdering.OrderMainGUI;
import com.accesshollywood.grfxOrdering.basics.BLOCK;
import com.accesshollywood.grfxOrdering.basics.Destinations;
import com.accesshollywood.grfxOrdering.basics.GraphicTypes;
import com.accesshollywood.grfxOrdering.basics.ShowNames;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.grfxOrdering.panels.DueDatePanel;
import com.accesshollywood.grfxOrdering.panels.ItemListPanel;
import com.accesshollywood.indexables.Destination;
import com.accesshollywood.indexables.GraphicType;
import com.accesshollywood.indexables.GrfxOrder;
import com.accesshollywood.indexables.ShowName;
import com.accesshollywood.indexables.User;
import com.accesshollywood.indexables.items.GrfxItem;
import com.accesshollywood.indexables.items.GrfxItemList;
import com.accesshollywood.indexables.items.TextIndexed;
import com.accesshollywood.notifications.Notify;
import com.accesshollywood.p2p.P2P;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class EditOrderDialog extends JDialog {
	public enum MODE {
		NEW_ORDER,
		EDIT_ORDER,
		ORIGINAL_ORDER;
	};

	private static final String			NEW_COMBO_ITEM	= "Add New...";

	private int							whichShowName	= 1;
	private EditOrderDialog				thisWindow;
	private OrderMainGUI				parent;
	public JPanel						contentPane;
	private JList						listGraphicType;
	private JTextField					txtSummary;
	private JTextField					txtFileName;
	private ComboBoxPlus				comboBoxProducer;
	private JCheckBox					chckbxNeedsFootage;
	private JCheckBox					chckbxNeedsPhotoEdit;
	private ComboBoxPlus				comboBoxResearch;
	private JCheckBox					chckbxNeedsResearch;
	private JCheckBox					chckbxNeedsLegal;
	private ComboBoxPlus				comboBoxPhotoEdit;
	private ComboBoxPlus				comboBoxArtist;
	private ComboBoxPlus				comboBoxShowName;
	private JButton						btnEditItem;
	private JButton						btnRemoveItem;
	private JPanel						itemPanel;
	private ItemListPanel				itemSubPanel;
	private JScrollPane					itemScrollPane;
	private JTextArea					txtNotes;
	private ComboBoxPlus				comboBoxDestination;
	private DueDatePanel				dueDatePanel;

	private GrfxOrder					thisOrder;

	private HashMap<Integer, GrfxItem>	itemSelection	= new HashMap<Integer, GrfxItem>();

	public MODE							mode;

	public EditOrderDialog(OrderMainGUI orderMainGUI) {
		super();
		mode = MODE.NEW_ORDER;
		//thisOrder = new GrfxOrder(true);
		thisOrder = new GrfxOrder();
		this.parent = orderMainGUI;
		createDialog();
		populateFromOrder(thisOrder);
	}

	public EditOrderDialog(OrderMainGUI orderMainGUI, GrfxOrder currOrder) {
		super();
		mode = MODE.EDIT_ORDER;
		thisOrder = currOrder;
		this.parent = orderMainGUI;
		createDialog();
		populateFromOrder(currOrder);
	}

	public void createDialog() {
		thisWindow = this;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 940, 729);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

		itemPanel = new JPanel();

		addItemSubPanel();

		JPanel graphicTypePanel = new JPanel();
		contentPane.add(graphicTypePanel);
		graphicTypePanel.setLayout(new BoxLayout(graphicTypePanel, BoxLayout.Y_AXIS));

		JPanel panel_4 = new JPanel();
		graphicTypePanel.add(panel_4);
		panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.X_AXIS));

		JLabel lblShow = new JLabel("  Show:");
		panel_4.add(lblShow);

		comboBoxShowName = new ComboBoxPlus(NEW_COMBO_ITEM, "com.accesshollywood.grfxOrdering.Dialogs.NewShowNameDialog");
		comboBoxShowName.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				whichShowName = ((ShowName) comboBoxShowName.getSelectedItem()).getId();
				setListModel();
			}
		});
		panel_4.add(comboBoxShowName);
		comboBoxShowName.setPreferredSize(new Dimension(75, 20));
		comboBoxShowName.setMaximumSize(new Dimension(32767, 20));
		comboBoxShowName.setModel(ShowNames.showNames);
		comboBoxShowName.setRenderer(comboBoxShowName.new ComboBoxPlusShowNameRenderer());

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		graphicTypePanel.add(scrollPane);

		listGraphicType = new JList();
		scrollPane.setViewportView(listGraphicType);
		setListModel();
		listGraphicType.setPreferredSize(new Dimension(100, 100));
		listGraphicType.setCellRenderer(new GraphicTypeListRenderer());
		listGraphicType.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listGraphicType.setEnabled(true);

		JPanel panel_3 = new JPanel();
		contentPane.add(panel_3);
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));

		JPanel summaryPanel = new JPanel();
		panel_3.add(summaryPanel);
		summaryPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		summaryPanel.setLayout(new BoxLayout(summaryPanel, BoxLayout.Y_AXIS));

		JPanel panel_1 = new JPanel();
		panel_1.setMaximumSize(new Dimension(32767, 25));
		summaryPanel.add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));

		JLabel lblBlock = new JLabel("  Block:");
		panel_1.add(lblBlock);

		JComboBox comboBoxBlock = new JComboBox(BLOCK.values());
		panel_1.add(comboBoxBlock);

		Component rigidArea_5 = Box.createRigidArea(new Dimension(20, 20));
		panel_1.add(rigidArea_5);

		JLabel lblSummary = new JLabel("Summary:");
		panel_1.add(lblSummary);

		txtSummary = new JTextField();
		txtSummary.setMaximumSize(new Dimension(2147483647, 20));
		panel_1.add(txtSummary);
		txtSummary.setColumns(10);

		JPanel panel = new JPanel();
		panel.setMaximumSize(new Dimension(32767, 25));
		summaryPanel.add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		JLabel lblDestination = new JLabel("Destination:");
		panel.add(lblDestination);

		comboBoxDestination = new ComboBoxPlus(NEW_COMBO_ITEM, "com.accesshollywood.grfxOrdering.Dialogs.NewDestinationDialog");
		panel.add(comboBoxDestination);
		comboBoxDestination.setMinimumSize(new Dimension(100, 18));
		comboBoxDestination.setPreferredSize(new Dimension(100, 20));
		comboBoxDestination.setModel(Destinations.destinations);
		comboBoxDestination.setRenderer(comboBoxDestination.new ComboBoxPlusDestinationRenderer());

		Component rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
		panel.add(rigidArea_1);

		JLabel lblProducer = new JLabel("  Producer:");
		panel.add(lblProducer);

		comboBoxProducer = new ComboBoxPlus(NEW_COMBO_ITEM, "com.accesshollywood.grfxOrdering.Dialogs.NewPersonDialog");
		panel.add(comboBoxProducer);
		comboBoxProducer.setModel(Users.producerUsers);
		comboBoxProducer.setRenderer(comboBoxProducer.new ComboBoxPlusUserRenderer());

		Component rigidArea_4 = Box.createRigidArea(new Dimension(20, 20));
		panel_3.add(rigidArea_4);

		JPanel bigGroupPanel = new JPanel();
		panel_3.add(bigGroupPanel);
		bigGroupPanel.setMinimumSize(new Dimension(10, 100));
		bigGroupPanel.setMaximumSize(new Dimension(32767, 100));
		bigGroupPanel.setAlignmentY(Component.TOP_ALIGNMENT);
		bigGroupPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		bigGroupPanel.setLayout(new BoxLayout(bigGroupPanel, BoxLayout.X_AXIS));

		dueDatePanel = new DueDatePanel();
		bigGroupPanel.add(dueDatePanel);

		JPanel needsSubPanel = new JPanel();
		needsSubPanel.setAlignmentY(Component.TOP_ALIGNMENT);
		bigGroupPanel.add(needsSubPanel);
		needsSubPanel.setLayout(new BoxLayout(needsSubPanel, BoxLayout.Y_AXIS));

		chckbxNeedsResearch = new JCheckBox("Needs Research");
		needsSubPanel.add(chckbxNeedsResearch);

		chckbxNeedsPhotoEdit = new JCheckBox("Needs PhotoEdit");
		needsSubPanel.add(chckbxNeedsPhotoEdit);

		chckbxNeedsFootage = new JCheckBox("Needs Footage");
		needsSubPanel.add(chckbxNeedsFootage);

		chckbxNeedsLegal = new JCheckBox("Needs Legal");
		needsSubPanel.add(chckbxNeedsLegal);

		Component horizontalGlue = Box.createHorizontalGlue();
		bigGroupPanel.add(horizontalGlue);

		JPanel panel_2 = new JPanel();
		panel_2.setAlignmentY(Component.TOP_ALIGNMENT);
		bigGroupPanel.add(panel_2);
		panel_2.setBorder(new TitledBorder(null, "Pre-Assign", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(150px;min)"), }, new RowSpec[] { RowSpec.decode("20px"),
				RowSpec.decode("20px"), FormFactory.DEFAULT_ROWSPEC, RowSpec.decode("20px"), }));

		JLabel lblNewLabel_1 = new JLabel("Research:");
		panel_2.add(lblNewLabel_1, "2, 1, left, center");

		comboBoxResearch = new ComboBoxPlus(NEW_COMBO_ITEM, "com.accesshollywood.grfxOrdering.Dialogs.NewPersonDialog");
		comboBoxResearch.setPreferredSize(new Dimension(200, 20));
		comboBoxResearch.setMinimumSize(new Dimension(100, 18));
		panel_2.add(comboBoxResearch, "4, 1, left, center");

		JLabel lblPhotedit = new JLabel("PhotoEdit:");
		panel_2.add(lblPhotedit, "2, 2, left, center");

		comboBoxPhotoEdit = new ComboBoxPlus(NEW_COMBO_ITEM, "com.accesshollywood.grfxOrdering.Dialogs.NewPersonDialog");
		comboBoxPhotoEdit.setPreferredSize(new Dimension(200, 20));
		comboBoxPhotoEdit.setMinimumSize(new Dimension(100, 18));
		panel_2.add(comboBoxPhotoEdit, "4, 2, left, center");

		JLabel lblArtist = new JLabel("Artist:");
		panel_2.add(lblArtist, "2, 3, left, center");

		comboBoxArtist = new ComboBoxPlus(NEW_COMBO_ITEM, "com.accesshollywood.grfxOrdering.Dialogs.NewPersonDialog");
		comboBoxArtist.setMinimumSize(new Dimension(100, 18));
		comboBoxArtist.setPreferredSize(new Dimension(200, 20));
		panel_2.add(comboBoxArtist, "4, 3, left, center");
		comboBoxResearch.setModel(Users.researchUsers);
		comboBoxPhotoEdit.setModel(Users.photoEditUsers);
		comboBoxArtist.setModel(Users.artistUsers);
		comboBoxResearch.setRenderer(comboBoxResearch.new ComboBoxPlusUserRenderer());
		comboBoxPhotoEdit.setRenderer(comboBoxPhotoEdit.new ComboBoxPlusUserRenderer());
		comboBoxArtist.setRenderer(comboBoxArtist.new ComboBoxPlusUserRenderer());

		Component verticalStrut = Box.createVerticalStrut(20);
		panel_3.add(verticalStrut);

		panel_3.add(itemPanel);
		itemPanel.setBorder(new TitledBorder(null, "Items:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.X_AXIS));

		itemPanel.setPreferredSize(new Dimension(100, 500));

		JPanel addRemoveItemPanel = new JPanel();
		itemPanel.add(addRemoveItemPanel);
		addRemoveItemPanel.setLayout(new BoxLayout(addRemoveItemPanel, BoxLayout.Y_AXIS));

		JButton btnAddItem = new JButton("Add Item");
		btnAddItem.setMaximumSize(new Dimension(97, 23));
		btnAddItem.setMinimumSize(new Dimension(97, 23));
		btnAddItem.setPreferredSize(new Dimension(97, 23));
		btnAddItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Trace.info("clickity");
				newItem();
			}
		});
		addRemoveItemPanel.add(btnAddItem);

		btnEditItem = new JButton("Edit Items");
		btnEditItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				editSelectedItem();
			}
		});

		Component rigidArea_9 = Box.createRigidArea(new Dimension(20, 20));
		addRemoveItemPanel.add(rigidArea_9);
		btnEditItem.setEnabled(false);
		addRemoveItemPanel.add(btnEditItem);

		btnRemoveItem = new JButton("Remove Items");
		btnRemoveItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				removeSelectedItem();
			}
		});

		Component rigidArea_8 = Box.createRigidArea(new Dimension(20, 20));
		addRemoveItemPanel.add(rigidArea_8);
		btnRemoveItem.setEnabled(false);
		addRemoveItemPanel.add(btnRemoveItem);

		Component verticalGlue = Box.createVerticalGlue();
		panel_3.add(verticalGlue);

		JPanel notesPanel = new JPanel();
		panel_3.add(notesPanel);
		notesPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		notesPanel.setBorder(new TitledBorder(null, "Notes:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		notesPanel.setLayout(new BoxLayout(notesPanel, BoxLayout.X_AXIS));

		txtNotes = new JTextArea();
		txtNotes.setMaximumSize(new Dimension(2147483647, 80));
		txtNotes.setMinimumSize(new Dimension(4, 80));
		txtNotes.setColumns(75);
		txtNotes.setRows(5);
		txtNotes.setLineWrap(true);

		JScrollPane scrollPane_1 = new JScrollPane(txtNotes);
		scrollPane_1.setMinimumSize(new Dimension(23, 80));
		scrollPane_1.setMaximumSize(new Dimension(32767, 80));
		scrollPane_1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane_1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		notesPanel.add(scrollPane_1);

		Component rigidArea_19 = Box.createRigidArea(new Dimension(20, 20));
		panel_3.add(rigidArea_19);

		JPanel OK_CancelPanel = new JPanel();
		panel_3.add(OK_CancelPanel);
		OK_CancelPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		OK_CancelPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		{
			JButton okButton = new JButton("OK");
			// okButton.setActionCommand("OK");
			OK_CancelPanel.add(okButton);
			getRootPane().setDefaultButton(okButton);
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					OK();
				}
			});
		}
		{
			JButton cancelButton = new JButton("Cancel");
			// cancelButton.setActionCommand("Cancel");
			OK_CancelPanel.add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					cancel();
				}
			});
		}

		if (Users.currentUser.isPhotoEdit()) {
			chckbxNeedsResearch.setEnabled(false);
			chckbxNeedsFootage.setEnabled(false);
			chckbxNeedsLegal.setEnabled(false);
			comboBoxArtist.setEnabled(false);
			comboBoxResearch.setEnabled(false);
		}

		setLocationRelativeTo(null);

		resetPanel();
	}

	public void setListModel() {
		switch (whichShowName) {
			case 1:
				listGraphicType.setModel(GraphicTypes.AHGraphicTypesModel);
				break;
			case 2:
				listGraphicType.setModel(GraphicTypes.LIVEGraphicTypesModel);
				break;
			default:
				listGraphicType.setModel(GraphicTypes.graphicTypesModel);
				break;
		}
	}

	public void populateFromOrder(GrfxOrder currOrder) {
		txtSummary.setText(currOrder.getSummary().toString());

		comboBoxShowName.setSelectedItem(currOrder.getShowName());
		// comboBoxGraphicType.setSelectedItem(currOrder.graphicType);
		listGraphicType.setSelectedValue(currOrder.getGraphicType(), true);
		comboBoxProducer.setSelectedItem(currOrder.getProducer());

		chckbxNeedsFootage.setSelected(currOrder.isNeedsFootage());
		chckbxNeedsPhotoEdit.setSelected(currOrder.isNeedsPhotoEdit());
		chckbxNeedsResearch.setSelected(currOrder.isNeedsResearch());
		chckbxNeedsLegal.setSelected(currOrder.isNeedsLegal());

		comboBoxResearch.setSelectedItem(currOrder.getResearcher());
		comboBoxPhotoEdit.setSelectedItem(currOrder.getPhotoEdit());
		comboBoxArtist.setSelectedItem(currOrder.getArtist());

		dueDatePanel.dateTimePicker.setDate(currOrder.getDueDate().rawDateTime.toDate());
		if (currOrder.getNotes() != null) {
			txtNotes.setText(currOrder.getNotes().toString());
		}
		comboBoxDestination.setSelectedItem(currOrder.getDestination());
	}

	public void OK() {
		GrfxOrder newOrder = makeOrder();
		try {
			switch (mode) {
				case NEW_ORDER:
					parent.addOrder(newOrder);
					P2P.refreshPeers();
					break;
				case EDIT_ORDER:
					compareAndAlert(newOrder);
					parent.editOrder(newOrder);
					P2P.refreshPeers(new int[]{newOrder.getId()});
					break;
				case ORIGINAL_ORDER:
					break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		parent.orderListPanelTop.refreshOrderGUI(true);
//		parent.orderListPanelBottom.refreshOrderGUI(true);
		thisWindow.dispose();
	}

	private void compareAndAlert(GrfxOrder newOrder) {
		if (thisOrder != null && newOrder != null && !thisOrder.equals(newOrder)) {
			Notify.orderModified(thisOrder, newOrder, Users.currentUser);
		}
	}

	public GrfxOrder makeOrder() {
		GrfxOrder newOrder = new GrfxOrder();
		// if (thisOrder == null) {
		// thisOrder = new GrfxOrder();
		// }
		
		// THESE SHOULD ALL BE QUIET SETTERS...
		newOrder.setId(thisOrder.getId());
		newOrder.setSummary(new TextIndexed(thisOrder.getId(), txtSummary.getText()));
		newOrder.setDueDate(new GrfxDateTime(dueDatePanel.dateTimePicker));
		newOrder.setGraphicType((GraphicType) listGraphicType.getSelectedValue());
		newOrder.setShowName((ShowName) comboBoxShowName.getSelectedItem());

		newOrder.setResearcher((User) comboBoxResearch.getSelectedItem());
		newOrder.setArtist((User) comboBoxArtist.getSelectedItem());
		newOrder.setProducer((User) comboBoxProducer.getSelectedItem());
		newOrder.setLegal(Users.allUsers.get(0));
		newOrder.setOrderedBy(Users.currentUser);
		newOrder.setPhotoEdit((User) comboBoxPhotoEdit.getSelectedItem());
		newOrder.setFootage(Users.allUsers.get(0));

		newOrder.setNeedsResearch(chckbxNeedsResearch.isSelected());
		newOrder.setNeedsPhotoEdit(chckbxNeedsPhotoEdit.isSelected());
		newOrder.setNeedsFootage(chckbxNeedsFootage.isSelected());
		newOrder.setNeedsLegal(chckbxNeedsLegal.isSelected());

		newOrder.setDestination((Destination) comboBoxDestination.getSelectedItem());
		newOrder.setNotes(new TextIndexed(thisOrder.getId(), txtNotes.getText()));
		GrfxItemList items = thisOrder.getItems();
		
		newOrder.setItems(items);
		//for(GrfxItem currItem : items.values()){
		Iterator itr = items.iterator();

		while(itr.hasNext()){
			((GrfxItem) itr.next()).setGrfxOrder(newOrder);
		}
		
		autoSetApprovals(newOrder);
		return newOrder;
	}

	public void cancel() {
		// parent.addOrder(thisOrder, MODE.ORIGINAL_ORDER);
		thisWindow.dispose();
	}

	public void resetPanel() {
		comboBoxShowName.setSelectedIndex(0);
		// comboBoxGraphicType.setSelectedIndex(0);
		comboBoxProducer.setSelectedIndex(0);
		txtSummary.setText("");
		dueDatePanel.comboBoxQuickDateTime.setSelectedIndex(0);
		chckbxNeedsResearch.setSelected(false);
		comboBoxResearch.setSelectedIndex(0);
		chckbxNeedsPhotoEdit.setSelected(false);
		chckbxNeedsFootage.setSelected(false);
		chckbxNeedsLegal.setSelected(false);
	}

	public void newItem() {
		NewItemDialog newItemDialog = new NewItemDialog(thisWindow, new GrfxItem());
		newItemDialog.setVisible(true);
	}

	public void editSelectedItem() {
		GrfxItem selectedItem = null;
		for (Map.Entry<Integer, GrfxItem> entry : itemSelection.entrySet()) {
			int key = entry.getKey();
			selectedItem = entry.getValue();
		}

		NewItemDialog newItemDialog = new NewItemDialog(thisWindow, selectedItem);
		newItemDialog.setVisible(true);
	}

	public void addItem(GrfxItem newItem, NewItemDialog.ITEM_MODE mode) {
		// Trace.info("adding item");
		switch (mode) {
			case NEW_ITEM:
//				thisOrder.getItems().add(newItem);
				break;
			case EDIT_ITEM:
//				thisOrder.getItems().add(newItem);
				break;
			case ORIGINAL_ITEM:
				break;
		}

		itemPanel.remove(itemScrollPane);
		// itemSubPanel = new ItemPanel(thisOrder, true, new
		// SecretCheckActionListener());
		addItemSubPanel();

		autoSetApprovals(thisOrder);
		contentPane.revalidate();
		itemPanel.revalidate();
		itemPanel.repaint();
	}

	public void removeSelectedItem() {
		for (Map.Entry<Integer, GrfxItem> entry : itemSelection.entrySet()) {
			int key = entry.getKey();
			GrfxItem value = entry.getValue();
			removeItem(value);
		}
		itemPanel.remove(itemScrollPane);
		// itemSubPanel = new ItemPanel(thisOrder, true, new
		// SecretCheckActionListener());
		addItemSubPanel();
		itemPanel.revalidate();
		itemPanel.repaint();
	}

	public void removeItem(GrfxItem removeItem) {
		// Trace.info(thisOrder.getItems().size() + "   " + removeItem);
		thisOrder.getItems().remove(removeItem.getId());
	}

	public void autoSetApprovals(GrfxOrder whichOrder) {
		boolean temp_needsResearch = chckbxNeedsResearch.isSelected();
		boolean temp_needsPhotoEdit = chckbxNeedsPhotoEdit.isSelected();
		boolean temp_needsFootage = chckbxNeedsFootage.isSelected();

		//for (Map.Entry<Integer, GrfxItem> entry : whichOrder.getItems().entrySet()) {
		Iterator itr = whichOrder.getItems().iterator();
		while(itr.hasNext()){
			GrfxItem currItem = (GrfxItem) itr.next();
			if (currItem.getTextList() != null && currItem.getTextList().size() > 0) {
				currItem.setNeedsResearch(true);
			} else {
				currItem.setNeedsResearch(false);
			}
			if (currItem.getStillList() != null && currItem.getStillList().size() > 0) {
				currItem.setNeedsPhotoEdit(true);
			} else {
				currItem.setNeedsPhotoEdit(false);
			}
			if (currItem.getFootageList() != null && currItem.getFootageList().size() > 0) {
				currItem.setNeedsFootage(true);
			} else {
				currItem.setNeedsFootage(false);
			}
			temp_needsResearch = temp_needsResearch | currItem.needsResearch();
			temp_needsPhotoEdit = temp_needsPhotoEdit | currItem.needsPhotoEdit();
			temp_needsFootage = temp_needsFootage | currItem.needsFootage();
		}

		chckbxNeedsResearch.setSelected(temp_needsResearch);
		chckbxNeedsPhotoEdit.setSelected(temp_needsPhotoEdit);
		chckbxNeedsFootage.setSelected(temp_needsFootage);
	}

	public void populateComboBox(JComboBox comboBox, String tableName, String fieldName, String where) {
		String[] rawList = GFXSQL.getString(tableName, fieldName, where);
		DefaultComboBoxModel model = new DefaultComboBoxModel();

		for (int i = 0; i < rawList.length; i++) {
			model.addElement(rawList[i]);
		}

		comboBox.setModel(model);
	}

	public void addItemSubPanel() {
		itemSubPanel = new ItemListPanel(thisOrder, true, new SecretCheckActionListener());
		itemSubPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		itemSubPanel.setBorder(null);
		itemScrollPane = new JScrollPane(itemSubPanel);
		itemPanel.add(itemScrollPane, 0);
	}

	public class SecretCheckActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			JCheckBox currCheck = (JCheckBox) e.getSource();
			int index = Integer.parseInt(currCheck.getText());
			if (currCheck.isSelected()) {
				itemSelection.put(index, ((Map<Integer, GrfxItem>) thisOrder.getItems()).get(index));
			} else if (itemSelection.containsKey(index)) {
				itemSelection.remove(index);
			}
			if (itemSelection.size() > 0) {
				btnRemoveItem.setEnabled(true);
			} else {
				btnRemoveItem.setEnabled(false);
			}
		}
	}

	public class GraphicTypeListRenderer extends DefaultListCellRenderer {
		protected DefaultListCellRenderer	defaultRenderer	= new DefaultListCellRenderer();

		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

			Font theFont = null;
			Color theForeground = null;
			String theText = null;

			JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

			if (value instanceof GraphicType) {
				GraphicType currGraphicType = (GraphicType) value;
				renderer.setText(currGraphicType.getName());
			} else {
				renderer.setText((String) value);
			}
			if (!isSelected) {
				renderer.setForeground(Color.BLACK);
			} else {
				renderer.setForeground(Color.WHITE);
			}
			return renderer;
		}
	}
}
